//
//  SADataBaseClient.m
//  SHISEIDO_Consultation
//
//  Created by brave on 11/04/12.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SADataBaseClient.h"
#import "DBDefine.h"


NSString * const SADBClientException = @"SADBClientException";

@interface SADataBaseClient ()

@property(nonatomic,retain)NSString *innerDBfileName;
@property(nonatomic,retain)FMDatabase *innerDB;
@property(nonatomic,assign)DBStatus status;

@end

@implementation SADataBaseClient

@synthesize innerDBfileName;
@synthesize innerDB;
@synthesize status;
@synthesize delegate;


- (id)init {
    return [ self initWithDBFileName:DBname ];
}

// init DB
- (id)initWithDBFileName:(NSString *)name{
    
    if((self = [ super init ])){
        self.innerDBfileName = name;
        
        NSLog(@"name = %@",name);
        
        if (name) {
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths lastObject];
            NSString *dbPath = [documentsDirectory stringByAppendingPathComponent:DBname];
            
			self.innerDB = [FMDatabase databaseWithPath:dbPath];
		} else {
            //			[NSException raise:BSFileNotFoundException 
            //						format:@"%@.%@ cannot be found in resources.", name, type];
		}
    }
    return self;
}

- (void)dealloc {
	// clean up
	if (self.status == DBStatusOpen) {
		[self close];
	}
	[innerDB release];
	[super dealloc];
}


- (NSString *)getDBfileName {
    return self.innerDBfileName;
}

- (void)open {
	if( ![self.innerDB open] ) {
		[NSException raise:SADBClientException
					format:@"Cannot open database."];
		self.status = DBStatusOpen;
         NSLog(@"DB_open");
	}
}

- (void)close {
    NSLog(@"DB_close");
	[self.innerDB close];
	self.status = DBStatusClosed;
}

// 初期化メソッド
-(void)Initialization{
    [ delegate endInitialization ];
}


@end
